<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-新增</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<div class="easy-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label required">字典类型</label>
            <div class="layui-input-block">
                <select name="dictTypeId" lay-verify="required" th:with="typeList=${@dictService.listType()}">
                    <option th:each="type : ${typeList}" th:text="${type.dictName}" th:value="${type.id}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">字典项</label>
            <div class="layui-input-block">
                <input type="text" name="dictItem" lay-verify="required" placeholder="请输入" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">字典值</label>
            <div class="layui-input-block">
                <input type="text" name="dictValue" lay-verify="required" placeholder="请输入" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">排序号</label>
            <div class="layui-input-block">
                <input type="number" name="orderNum" lay-verify="required" autocomplete="off" class="layui-input" step="1" lay-affix="number">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm">新建</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="cancelForm">取消</button>
            </div>
        </div>
    </form>
</div>
</body>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'layer', 'table', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var form = layui.form;

        // 保存
        form.on('submit(submitForm)', function (form) {
            var data = form.field;
            $.ajax({
                url: '/sysDictData/add',
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (response) {
                    if (response.code === 200) {
                        // 关闭
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        // 刷新
                        // parent.location.reload();

                        layer.msg('新建成功', {icon: 1});
                    } else {
                        layer.msg(response.msg, {icon: 0});
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('请求失败', {icon: 0});
                }
            });
            return false;
        });

        // 取消
        $('#cancelForm').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });

    });
</script>
</html>